package com.mobi.Survey.uicomponents;

import java.util.ArrayList;
import java.util.StringTokenizer;

import android.content.Context;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.mobi.Survey.controller.SurveyDetailPojo;
/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * CustomSpinner.java
 * 
 * 
 */
public class CustomSpinner extends Spinner
{
	private static final String DELIM = "|";
	private LinearLayout ll;
	private Context ctx;

	public CustomSpinner(Context context, SurveyDetailPojo sdp, boolean ediable)
	{
		super(context);
		this.ctx = context;
		ll = new LinearLayout(ctx);

		ll.setOrientation(LinearLayout.VERTICAL);
		if (sdp.getControlText() != null && sdp.getControlText().length() > 0)
		{
			CustomTextView txt = new CustomTextView(ctx);
			txt.setCustomColor(sdp);
			txt.setText(sdp.getControlText());
			ll.addView(txt);

		}
		String vals = sdp.getControlParameter();
		if (vals == null)
			vals = DELIM;

		StringTokenizer st = new StringTokenizer(vals, DELIM);
		if (st != null)
		{
			ArrayList<String> str = new ArrayList<String>();
			int i = 0;
			int selection = 0;
			while (st.hasMoreTokens())
			{
				String token = st.nextToken().trim();
				if (sdp.getUserValue().equalsIgnoreCase(token))
				{
					selection = i;
				}
				i++;
				str.add(token);
			}

			fillSpinner(str);
			this.setSelection(selection);
		}
		if (!ediable)
		{
			this.setEnabled(false);
		}
		ll.addView(this);

	}

	public View getView()
	{
		return ll;
	}

	protected void fillSpinner(ArrayList<String> values)
	{
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(ctx,
				android.R.layout.simple_spinner_item, values);
		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		setAdapter(dataAdapter);

	}

}
